// pages/instdetail/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showDialog: true,
		showauth: false, //显示授权登陆
		alldetail: [], //课程详情
		courseId: 0, //课程id
		compareNum: 0, //课程比较数量
		compareshow: false, //显示课程数量
		comments: [], //评论列表
		textelllipsis: true, //多行省略  
		imgUrls: [],
		sharetip: false, //分享弹窗,
		stateNum: 0
	},
	// 课程保存
	changeClass: function(e) {
		console.log(e)
		let status = e.currentTarget.dataset.status;
		let tips = '确认停售该课程吗？';
		let stateNum = 3;
		if(status == 3) {
			tips = '确认上线该课程吗？';
			stateNum = 5;
		}
		let that = this
		wx.showModal({
			title: '提示',
			content: tips,
			success: function(e){
				if(e.confirm) {
					var coursesava = loginFn()
					coursesava.courseId = that.data.alldetail.courseId
					coursesava.status = status;
					wx.request({
						url: urls.courseSave,
						data: coursesava,
						success: function(res) {
							that.setData({
								stateNum: res.data.data.status
							})
						}
					})
				}
			}
		});
		
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseId = options.courseId
		this.setData({
			courseId: options.courseId
		})

		//课程详情
		this.getcourseInfo(loginInfo)
		// this.courseSave(options)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var compareNum = wx.getStorageSync('compare')
		console.log(compareNum)
		if (compareNum) {
			console.log(compareNum)

			this.setData({
				compareNum: compareNum.length
			})
		};
		//评论列表
		this.getconmentFn()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		this.setData({
			sharetip: false
		})
		return {
			title: this.data.alldetail.courseName,
			path: '/pages/classdetail/index?courseId='+this.data.courseId,
			imgUrl: this.data.alldetail.uploadImg
		}
	},
	//分享弹框
	shareFn: function() { 
		this.setData({
			sharetip: true
		})
	},
	//分享到朋友圈
	shareCircle: function() { 
		wx.navigateTo({
			url: '/pages/share/index?type=class',
		})
	},
	// 获取课程详情
	getcourseInfo: function(loginInfo) {
		var that = this
		loginInfo.isAdmin=1
		wx.request({
			url: urls.courseInfo,
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					console.log(res.data.data)
					var datas = res.data.data
					for (var i = 0; i < app.globalData.classtype.courseBase.length; i++) {
						if (app.globalData.classtype.courseBase[i].courseBaseId == datas.courseBaseId) {
							datas['courseBase'] = app.globalData.classtype.courseBase[i].courseBase
						};
					}
					for (var i = 0; i < app.globalData.classtype.coursePeople.length; i++) {
						if (app.globalData.classtype.coursePeople[i].coursePeopleId == datas.coursePeopleId) {
							datas.coursePeople = app.globalData.classtype.coursePeople[i].coursePeople
						};
					}
					for (var i = 0; i < app.globalData.allselect.nation.length; i++) {
						if (app.globalData.allselect.nation[i].nationId == datas.nationId) {
							datas.nation = app.globalData.allselect.nation[i].nation
						};
					}
					var imgUrls = []
					imgUrls.push(datas.uploadImg)

					for (var i = 0; i < datas.courseImages.length; i++) {
						imgUrls.push(datas.courseImages[i].img)
					}
					console.log(datas.status)
					that.setData({
						alldetail: datas,
						imgUrls: imgUrls,
						stateNum: datas.status
					})
					console.log(that.data.stateNum)
				};
			}
		})
	},
	//立即报名
	applyFn: function() { 
		var that = this;
		wx.navigateTo({
			url: '/pages/order/index?courseId=' + that.data.courseId,
		})
	},
	//編輯課程
	edit: function(e) {
		console.log(e)
		let id = e.currentTarget.dataset.id
		// return
		wx.navigateTo({
			url: '/pages/addclass/index?courseId=' + id+'&type=edit&schoolId='+this.data.alldetail.schoolId,
		})
	},

	wantFn: function() { //想学
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseId = this.data.alldetail.courseId
		wx.request({
			url: urls.courserwant,
			data: loginInfo,
			method: 'POST',
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					if (res.data.data.want == '1') {
						wx.showToast({
							title: '想让TA教',
						})
					} else {
						wx.showToast({
							title: '取消想让TA教',
							icon: 'none'
						})
					};
				};
			}
		})
	},
	getconmentFn: function() { //评论列表
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseId = this.data.courseId;
		wx.request({
			url: urls.listCourse,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					that.setData({
						comments: res.data.data.list
					})
				};
			}
		})
	},
	replyFn: function(e) { //回复评论
		var userInfo = wx.getStorageSync('userinfo')
		if (userInfo) {
			wx.navigateTo({
				url: '/pages/replay/index?parentRemarkId=' + e.currentTarget.dataset.id + '&courseId=' + this.data.courseId,
			})
		} else {
			this.setData({
				showauth: true
			})
		}

	},
	likeremarkFn: function(e) { //评论点赞
		var that = this;
		var userInfo = wx.getStorageSync('userinfo')
		if (userInfo) {
			var loginInfo = loginFn()
			loginInfo.remarkId = e.currentTarget.dataset.id
			wx.request({
				url: urls.likeCourse,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					if (res.data.code == '0') {
						that.getconmentFn()
						wx.showToast({
							title: '点赞成功',
						})
					};

				}
			})
		} else {
			this.setData({
				showauth: true
			})
		}

	},

	allellipsisFn: function() { //显示所有内容
		this.setData({
			textelllipsis: false
		})
	},
	ellipsisFn: function() { //隐藏所有内容
		this.setData({
			textelllipsis: true
		})
	},
	teachermeFn: function() { //已学
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseId = this.data.alldetail.courseId;
		wx.request({
			url: urls.learnCourse,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == 0) {
					wx.showToast({
						title: '成功',
					})
				}
			}
		})
	},
	hideMask(){
		this.setData({
			sharetip: false
		})
	},
	previewFn: function(e) { //预览图片
		wx.previewImage({
			current: e.currentTarget.dataset.url, // 当前显示图片的http链接
			urls: this.data.imgUrls // 需要预览的图片http链接列表
		})
	},

})
